#include "mylabel.h"
#include <QMouseEvent>
#include <QWheelEvent>
#include <QDebug>

MyLabel::MyLabel(QWidget* parent) : QLabel(parent)
{
    // 设置鼠标追踪状态，把这个选项设置为true，才能够追踪鼠标的移动位置
//    this->setMouseTracking(true);
}

void MyLabel::mousePressEvent(QMouseEvent *event)
{
    // 参数event对象就包含了鼠标点击位置的坐标以及鼠标哪个按键点击的
    if (event->button() == Qt::LeftButton) qDebug() << "按下左键";
    else if (event->button() == Qt::RightButton) qDebug() << "按下右键";
    else if (event->button() == Qt::MidButton) qDebug() << "按下滚轮";
    else if (event->button() == Qt::ForwardButton) qDebug() << "按下前进";
    else if (event->button() == Qt::BackButton) qDebug() << "按下后退";

    // x()和y()是以当前控件左上角为原点的坐标
//    qDebug() << event->x() << ", " << event->y();
    // Qt中的格式化字符串
    QString str = QString("x = %1, y = %2").arg(event->x()).arg(event->y());
    qDebug() << str;
    // globalX()和globalY()是以屏幕左上角为原点的坐标
    qDebug() << event->globalX() << ", " <<event->globalY();
}

void MyLabel::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) qDebug() << "释放左键";
    else if (event->button() == Qt::RightButton) qDebug() << "释放右键";
    else if (event->button() == Qt::MidButton) qDebug() << "释放滚轮";
    else if (event->button() == Qt::ForwardButton) qDebug() << "释放前进";
    else if (event->button() == Qt::BackButton) qDebug() << "释放后退";

    qDebug() << event->x() << ", " << event->y();
    qDebug() << event->globalX() << ", " <<event->globalY();
}

void MyLabel::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) qDebug() << "双击左键";
    else if (event->button() == Qt::RightButton) qDebug() << "双击右键";
    else if (event->button() == Qt::MidButton) qDebug() << "双击滚轮";
    else if (event->button() == Qt::ForwardButton) qDebug() << "双击前进";
    else if (event->button() == Qt::BackButton) qDebug() << "双击后退";
}

void MyLabel::mouseMoveEvent(QMouseEvent *event)
{
    qDebug() << event->x() << ", " << event->y();
    qDebug() << event->globalX() << ", " <<event->globalY();
}

void MyLabel::wheelEvent(QWheelEvent *event)
{
    qDebug() << event->delta();
}
